package com.tamartech.greendb.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.tamartech.greendb.data.Contact;


public class ContactDAO extends DAO
{
   public Contact save (Contact a) 
         throws Exception
   {
      try
      {
         begin();
         getSession().save(a);
         commit();
         return a;
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not create Contact", e);
      }
   }


//   public void update(Constituency a) throws Exception 
//   {
//      try 
//      {
//         begin();
//         getSession().update(a);
////         getSession().save(a);
//         commit();
//      } 
//      catch (HibernateException e) 
//      {
//         rollback();
//         throw new Exception("Could not save Constituency", e);
//      }
//   }

   public Contact get (Long id) throws Exception 
   {
      try 
      {
         begin();
         Contact a = (Contact) getSession().load(Contact.class, id);
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain Contact " + id, e);
      }
   }

   @SuppressWarnings("unchecked")
   public List<Contact> list() throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Contact");
         List<Contact> list = q.list();
         commit();
         return list;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not list the contacts", e);
      }
   }

   
   public void delete(Contact a)
      throws Exception
   {
      try
      {
         begin();
         getSession().delete(a);
         commit();
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not delete Contact", e);
      }
   }
}
